package com.dyz.swordFingerOffer;

/**
 * @author: daiyizheng
 * @date: 2021/5/23 14:40
 * @description:
 */
public class searchMatrix04 {

    public boolean findNumberIn2DArray(int[][] matrix, int target) {
        //暴力求解，两个for循环
        //特殊条件判断
        if (matrix==null || matrix.length==0 || matrix[0]==null || matrix[0].length==0){
            return false;
        }
        for(int i=0; i<matrix.length; i++){
            for(int j=0; j<matrix[0].length;j++){
                if(matrix[i][j]==target){
                    return true;
                }
            }
        }
        return false;

    }
    //优化 根据二维数组的递增排序特性
    public boolean findNumberIn2DArray1(int [][] matrix, int target){
        if (matrix==null || matrix.length==0 || matrix[0]==null || matrix[0].length==0){
            return false;
        }
        //两个嵌套遍历
        // - 一个遍历从左往右0~matrix.length
        // - 两个一从下到上matrix[0].length ~0
        int rows = matrix.length;
        int cols = matrix[0].length;
        //下标
        int row = 0;
        int col = cols-1;
        while(row<rows && col>=0){
            //是要找的target
            if(matrix[row][col]==target){
                return true;
            }
            //当前的数大于target,
            if(matrix[row][col]>target){
                col--;
            }else{
                row++;
            }
        }
        return false;
    }

}
